﻿Imports System.IO

Public Class frmMain

    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper

#Region "Function"

#End Region
    Private Sub BtnDB_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnDB.ItemClick
        With frmDBSetting
            .ShowDialog()
        End With
    End Sub

    Private Sub BtnExit_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnExit.ItemClick
        Me.Close()
    End Sub

    Private Sub BtnNewQuery_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnNewQuery.ItemClick
        Dim NewQuery As New frmquery
        With NewQuery
            .MdiParent = Me
            .WindowState = FormWindowState.Maximized
            .Show()
        End With
    End Sub

    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MsgBox("คุณต้องการออกจากโปรแกรมใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยัน") = MsgBoxResult.No Then
            e.Cancel = True
            Exit Sub
        End If
        Exit Sub
        For Each frm As Form In Application.OpenForms
            frm.Close()
        Next
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        With frmquery
            .MdiParent = Me
            .WindowState = FormWindowState.Maximized
            .Show()
        End With
    End Sub

    Private Sub BtnLoad_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnLoad.ItemClick

        With OpenFileDialog1
            .Title = "Save As"
            .FileName = "ExportData"
            .Filter = "SQL Files (*.sql)|*.sql"
            If .ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub
            Dim strContents As String
            Dim objReader As StreamReader
            Try

                objReader = New StreamReader(.FileName)
                strContents = objReader.ReadToEnd()
                objReader.Close()
                If frmquery.Visible = False Then
                    frmquery.MemoSql.Text = strContents
                    frmquery.MdiParent = Me
                    frmquery.WindowState = FormWindowState.Maximized
                    frmquery.Show()
                Else
                    frmquery.MemoSql.Text = strContents
                End If
            Catch Ex As Exception
                MsgBox(Ex.Message)
            End Try

        End With
    End Sub

    Private Sub BtnSave_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnSave.ItemClick
        If frmquery.Visible = False Then
            MsgBox("กรุณาเปิดไฟล์ Query ก่อนทำการบันทึก", MsgBoxStyle.Critical, "ไม่สามารถบันทึกได้")
            Exit Sub
        Else
            If MsgBox("คุณต้องการบันทึกข้อมูลคำสั่ง SQL นี้ใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub
            If frmquery.MemoSql.Text = "" Then
                MsgBox("กรุณาใส่คำสั่ง sql ก่อนทำการบันทึก", MsgBoxStyle.Exclamation, "ไม่สามารถบันทึกคำสั่ง sql ได้")
                Exit Sub
            End If
        End If

        With SaveFileDialog1
            .Title = "Save As"
            .FileName = "ExportData"
            .Filter = "SQL Files (*.sql)|*.sql"
            If .ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub

            '  If System.IO.File.Exists(.FileName) = True Then
            Dim objWriter As New System.IO.StreamWriter(.FileName)
            objWriter.Write(frmquery.MemoSql.Text)
            objWriter.Close()
            MsgBox("บันทึกคำสั่ง sql เรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการบันทึกคำสั่ง sql")
            ' Else
            '  MsgBox("File Does Not Exist")
            '  End If

        End With
    End Sub

    Private Sub BtnHelp_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnHelp.ItemClick
        frmTeamviewer.ShowDialog()
    End Sub

    Private Sub BtnAbout_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnAbout.ItemClick
        frmAbout.ShowDialog()
    End Sub

    Private Sub BtnBug_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnBug.ItemClick
        MsgBox("Email to farmonline@gmail.com", MsgBoxStyle.Information)
    End Sub

    Private Sub BtnExitProgram_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnExitProgram.ItemClick
        Me.Close()
    End Sub

 
    Private Sub BtnTileVertical_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnTileVertical.ItemClick
        LayoutMdi(MdiLayout.TileVertical)
    End Sub

    Private Sub BtnTileHorizontal_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnTileHorizontal.ItemClick
        LayoutMdi(MdiLayout.TileHorizontal)
    End Sub

    Private Sub BtnResetWindowsLayout_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)

    End Sub

   

    Private Sub BtnCascade_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnCascade.ItemClick
        LayoutMdi(MdiLayout.Cascade)
    End Sub

    Private Sub BtnCloseAll_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnCloseAll.ItemClick
        For Each ChildForm As Form In Me.MdiChildren
            ChildForm.Close()
        Next
    End Sub
End Class